﻿

function addSelectAction(map){
	
	
	var select = new ol.interaction.Select({
		//condition: ol.events.condition.pointerMove,
  	style: overlayStyle
	});
	select.on('select', function(e) {
	
 		var features = select.getFeatures();
    
    if(features.length < 1) return;
   	var arr = features.getArray();
   	var selId = arr[0].getId();    
   	  	
    
   /* var layer = select.getLayer(arr[0]);
    deleteFeature(layer, selId);   
    features.forEach(features.remove, features);*/
    
	});
	
	map.addInteraction(select);
}


var nodelayer = null;
function registerClickEvent( getPopup){
	
	
	nodelayer = getNodeLayer();
	map.on('click', function(e) {
	       
	       
	       
		var feature = map.forEachFeatureAtPixel(e.pixel, 
											function(feature){
												return feature;
											});
		if(feature)	{
			
			var coordinates = feature.getGeometry().getCoordinates();
			var name = feature.get('name');
			
			var coordinate = coordinates;
			var type = feature.getGeometry().getType();
			
			if(type == "Polygon" 
				||type == "LineString"){
				coordinate = e.coordinate;
				
				highNodes(nodelayer, coordinates);
			}
			popupInfo( coordinate, name );
			
		}
		else{
			
			highNodes(nodelayer, null);
			popupInfo( null,  null );
			
			//var coordinate = e.coordinate;
			
			//testCreate(coordinate);
		
		}
	});
}

function registerPointerMoveEvent(fieldname){

	
	this.field = fieldname;
	map.on('pointermove', function(e) {
		var pixel = map.getEventPixel(e.originalEvent);
		var hit = map.hasFeatureAtPixel(pixel);
	        
		var element = map.getTargetElement();
				element.style.cursor = hit ? 'pointer' : '';
				
		if(hit){
			
			var feature = map.forEachFeatureAtPixel(e.pixel, 
											function(feature){
												return feature;
											});
											
			var geom = feature.getGeometry();
			var name = feature.get(this.field);
			if(name == null){
				name = feature.get('name');
			}

			
			var coordinate = null;
			if(geom instanceof ol.geom.Polygon
				||geom instanceof ol.geom.MultiPolygon ){

				var extent = geom.getExtent();
				coordinate = [(extent[0]+extent[2])/2, (extent[1]+extent[3])/2];
			}
			else if(geom instanceof ol.geom.LineString){
						coordinate = geom.getLastCoordinate();
					}
			else{
					coordinate = geom.getCoordinates();

			}			
			popupInfo( coordinate, name );
		}
		else{
					popupInfo( null,  null );
			}
				
	}, this);

}


function SelectFeatureEvent(curmap){
	
	this.map = curmap;
	this.overlayContent = null;
	this.selectFeatue = null;
	this.overlay = 	null;
	this.container = null;
	this.closer = null;
}

	SelectFeatureEvent.prototype = {
		
			selectEvent:function(callbackFun){
				
				this.overlayContent = callbackFun;
				
				var container = document.getElementById('popup');
      	this.content = document.getElementById('popup-content');
      	this.closer = document.getElementById('popup-closer');
      	
				this.selectFeatue = new ol.interaction.Select({
        										wrapX: false
      										});
      	this.overlayContent = callbackFun;
				this.map.addInteraction(this.selectFeatue);
				
				this.overlay = new ol.Overlay({
       								 	element: container,
        								autoPan: true,
        								autoPanAnimation: {
          											duration: 250
        								},
        								positioning: 'bottom-center',
												stopEvent:false,
												offset:[0,-10]		
        							});
        this.map.addOverlay(this.overlay);
        
        var over_pop = this.overlay;
        var over_closer = this.closer;
        /**
       	* Add a click handler to hide the popup.
      	 * @return {boolean} Don't follow the href.
       	*/
      	this.closer.onclick = function() {
        	over_pop.setPosition(undefined);
        	over_closer.blur();
        	return false;
     		};
				  //选中矢量Feature显示气泡详情
    		this.selectFeatue.on('select', function(evt){
    	
        		var feature = evt.selected[0];
    	
    				if (feature) {
    						var coordinate = evt.mapBrowserEvent.coordinate;
            		var contentHtml = this.overlayContent(feature);//创建气泡里显示信息内容
            		this.content.innerHTML = contentHtml;
            		this.overlay.setPosition(coordinate);
           	}
    		}, this);
				
			},
			
			removeSelectEvent:function(){				
					this.map.removeInteraction(this.selectFeatue);				
				
			},
			
			adddSelectEvent:function(){				
					this.map.addInteraction(this.selectFeatue);			
				
			}
    	 
	}